package com.nfu.zlsp.dev.controller;

import com.nfu.zlsp.dev.common.constants.TheConstant;
import com.nfu.zlsp.dev.common.utils.PageUtils;
import com.nfu.zlsp.dev.common.utils.R;
import com.nfu.zlsp.dev.entity.UserEntity;
import com.nfu.zlsp.dev.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Map;



/**
 * 
 *
 * @author txh
 * @email hunterapeman@gmail.com
 * @date 2023-03-12 01:54:16
 */
@RestController
@RequestMapping("dev/user")
public class UserController {
    @Autowired
    private UserService userService;


    /**
     * 登录
     * @param user
     * @return
     */

    @RequestMapping("/login")
    public R login(@RequestBody UserEntity user, HttpServletResponse response, HttpSession session){
        UserEntity userEntity = userService.queryUserForLogin(user);
        if(userEntity != null){


            //1:托运方   2:承运方
            if (userEntity.getUserType() == 1){
                session.setAttribute(TheConstant.USER_INFO,userEntity);
            }else if (userEntity.getUserType() == 2){
                session.setAttribute(TheConstant.USER_INFO_TRANSPORT,userEntity);
            }

            if(user.getIsRemMe() == true){

                //记住用户账户  把信息放入cookie
                Cookie c1 = new Cookie("loginActCookie",user.getNumber());
                c1.setMaxAge(10 * 24 * 60 * 60);
                c1.setPath("/");
                response.addCookie(c1);
                Cookie c2 = new Cookie("loginPwdCookie", user.getPassword());
                c2.setMaxAge(10 * 24 * 60 * 60);
                c2.setPath("/");
                response.addCookie(c2);
            }else{

                //删除cookie
                Cookie c1 = new Cookie("loginActCookie","1");
                c1.setMaxAge(0);
                c1.setPath("/");
                response.addCookie(c1);
                Cookie c2 = new Cookie("loginPwdCookie", "1");
                c2.setMaxAge(0);
                c2.setPath("/");
                response.addCookie(c2);
            }

            return R.ok();
        }else {
            return R.ok().put("data","用户名或密码错误或用户类型错误");
        }

    }






    /**
     * 修改密码
     */
    @RequestMapping("/updatePwdById")
    public R updatePwdById(@RequestParam Map<String, Object> params){
        R r = userService.updatePwdById(params);
        return r;
    }


    /**
     * 获取当前用户信息
     * @return
     */
    @RequestMapping("/getUserInfo")
    public R getUserInfo(HttpSession session){
        UserEntity user = (UserEntity) session.getAttribute(TheConstant.USER_INFO);
        if(user == null){
            return R.ok().put("data","登录信息过期，请重新登录用户");
        }
        UserEntity userEntity = userService.getById(user.getId());
        return R.ok().put("user",userEntity);
    }

    /**
     * 获取当前用户信息
     * @return
     */
    @RequestMapping("/getUserInfoTransport")
    public R getUserInfoTransport(HttpSession session){
        UserEntity user = (UserEntity) session.getAttribute(TheConstant.USER_INFO_TRANSPORT);
        if(user == null){
            return R.ok().put("data","登录信息过期，请重新登录用户");
        }
        UserEntity userEntity = userService.getById(user.getId());
        return R.ok().put("user",userEntity);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		UserEntity user = userService.getById(id);
        //userService.queryPage();
        return R.ok().put("user", user);
    }

    /**
     * 注册
     */
    @RequestMapping("/register")
    public R save(@RequestBody UserEntity user){
        String isSuccess = userService.saveUser(user);
        if(isSuccess == TheConstant.RETURN_SUCCESS){
            return R.ok();
        }else {
            return R.ok().put("data","该用户已经被注册,可直接登录");
        }

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("dev:user:delete")
    public R delete(@RequestBody String[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
